#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
创建爱国情感分析表的数据库迁移脚本
"""

import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from app import app
from exts import db
from models import PatrioticAnalysis

def create_patriotic_analysis_table():
    """创建爱国情感分析表"""
    with app.app_context():
        try:
            # 创建表
            db.create_all()
            print("✅ 爱国情感分析表创建成功！")
            
            # 验证表是否存在
            from sqlalchemy import inspect
            inspector = inspect(db.engine)
            tables = inspector.get_table_names()
            
            if 'patriotic_analysis' in tables:
                print("✅ 表 'patriotic_analysis' 已成功创建")
                print("📋 表结构：")
                
                # 获取表的列信息
                columns = inspector.get_columns('patriotic_analysis')
                for column in columns:
                    print(f"   - {column['name']}: {column['type']}")
                    
            else:
                print("❌ 表创建失败")
                
        except Exception as e:
            print(f"❌ 创建表时出错: {str(e)}")
            db.session.rollback()

if __name__ == "__main__":
    print("🚀 开始创建爱国情感分析表...")
    create_patriotic_analysis_table()
    print("🎉 数据库迁移完成！")

